//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    bannerUrls: [],
    cutover: [
      { name: '商家导购', img: '/images/position/1.png', id: 1, url: '/pages/guide/guide', type: 'cutover' },
      { name: '积分商城', img: '/images/position/2.png', id: 2, url: '/pages/integral/integral', type: 'cutover' },
      { name: '主题活动', img: '/images/position/3.png', id: 3, url: '/pages/about/about', type: 'cutovers' },
      { name: '领劵中心', img: '/images/position/4.png', id: 4, url: '/pages/coupons/coupons', type: 'cutovers' },
      { name: '自助积分', img: '/images/position/5.png', id: 5, url: '/pages/jifen/jifen', type: 'cutovers' },
      { name: '停车缴费', img: '/images/position/6.png', id: 6, url: '/pages/park/park', type: 'cutover' },
      { name: '限时秒杀', img: '/images/position/7.png', id: 7, url: '/pages/limitTimeBy/limitTimeBy', type: 'cutovers' },
      { name: '会员中心', img: '/images/position/8.png', id: 8, url: '/pages/my/my', type: 'cutover' },
    ],
    coupon: { img: '/images/banner/coupon.png'},
    search: { img: '/images/position/search.png'},
    miaosha1: { img: '/images/position/s_1.png' },
    miaosha2: { img: '/images/position/s_2.png' },
    time: { hour: 14, minute:15,second:16},
    m_product:[],
    product:[],
    support:app.d.support
  },
  //事件处理函数
  onLoad:function(){
    var that = this;
    
    that.setData({
      imgPath:app.d.imgPath
    })
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.request({
          //url: app.globalData.api_url + 'Homepage/openid',
          url: app.d.path + 'Index/openid',
          data: {
            code: res.code,
          },
          success: function (res) {
            console.log(res.data);
            var session_key = res.data.session_key;
            app.d.openid = res.data.openid;
            //that.init();
            wx.getSetting({
              success: res => {
                // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                var code = res.code;
                if (res.authSetting['scope.userInfo']) {
                  wx.getUserInfo({
                    success: function (res) {
                      console.log(res.userInfo);

                    }
                  });
                } else {
                  wx.reLaunch({
                    url: '/pages/login/login?code=' + code,
                  })
                }
              }
            })
          }
        })
      },
      fail: function () {
        console.log("登录失败")
      }
    });
    that.init();
  },
  init:function(){
    var that = this;
    var WxParse = require('../../wxParse/wxParse.js');
    wx.request({
      url: app.d.path +'/Index/index', //仅为示例，并非真实的接口地址
      data: {
       
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.setData({
          bannerUrls:res.data.banner,
          m_product: res.data.spike,
          product: res.data.integral,
          starttime: res.data.time
          //"product": res.data.product,
          //"bannerUrls": res.data.banner,
        })
        that.countdowns(res.data.time)
      },
      fail: function () {
        console.log(1);
      },
      complete:function(){
        wx.stopPullDownRefresh();
      }
    })
  },
  countdowns: function (time) {
    var that = this;
    if(time < 0){
      that.setData({
        'time.hour': '00',
        'time.minute': '00',
        'time.second': '00'
      })
      return
    }
    that.setData({
      'time.hour': Math.floor(time / 3600 % 24),
      'time.minute': Math.floor(time / 60 % 60),
      'time.second': Math.floor(time % 60)
    })
    setTimeout(function () {
      that.setData({
        starttime: that.data.starttime - 1
      });
      that.countdowns(that.data.starttime);
    }, 1000)
  },
  onPullDownRefresh: function () {
    this.init();
  },
  cutover: function (e) {
    var that = this;
    var url = e.currentTarget.id;
    wx.switchTab({
      url: url
    })
  },
  couponM:function(){
    wx.navigateTo({
      url: "/pages/coupons/coupons"
    })
  },
  cutovers: function (e) {
    var that = this;
    var url = e.currentTarget.id;
    wx.navigateTo({
      url: url
    })
  },
  showImg: function (e) {
    console.log(e);
    var url = e.currentTarget.dataset.id
    if(url){
      wx.navigateTo({
        url: "/pages/banner/banner?url=" + url
      })
    }
  },
  giveCall:function(){
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.tel //仅为示例，并非真实的电话号码
    })
  },
  onShareAppMessage: function (res) {
    return {
      title: app.d.shareTxt,
      path: '/pages/index/index',
      success: function (res) {
        //console.log("转发成功");
      },
      fail: function (res) {
        //console.log("转发失败")
      }
    }
  }

  
})
